<?php

require 'config.php';
require CAMINHO_SERVIDOR.'funcoes.php';
require 'dao/amigoDAO.class.php';
if (isset($_GET['id_user_facebook'])) {
    $format = strtolower($_GET['format']) == 'json' ? 'json' : 'xml'; //xml is the default
    $amigoDao = new amigoDAO();
    $user_id = $amigoDao->pegaIdUserByIdFace($_GET['id_user_facebook']);
    if (empty($user_id)) {
        $result=$amigoDao->gravarNovoUsuarioFacebook($_GET['id_user_facebook'], $_GET['name_user_facebook'], $_GET['email_user_facebook']);
    } else {
        $result = $user_id;
    }
    if ($format == 'json') {
        header('Content-type: application/json');
        $post = array();
        $post[] = array('id_usuario' => $result);
        echo json_encode(array('posts' => $post));
    } else {
        header('Content-type: text/xml');
        echo '<posts>';
        foreach ($posts as $index => $post) {
            if (is_array($post)) {
                foreach ($post as $key => $value) {
                    echo '<', $key, '>';
                    if (is_array($value)) {
                        foreach ($value as $tag => $val) {
                            echo '<', $tag, '>', htmlentities($val), '</', $tag, '>';
                        }
                    }
                    echo '</', $key, '>';
                }
            }
        }
        echo '</posts>';
    }
$amigoDao->fecharConexao();
}
?>